﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace console.demo.senior
{
    /// <summary>
    ///  异步编程
    /// </summary>
    public class AsyncDemo
    {
        public async Task<int> GetUrlContentLengthAsync()
        {
            var client = new HttpClient();
            Task<string> getStringTask = client.GetStringAsync("https://docs.microsoft.com/donet");
            DoIndependentWork();
            string contents = await getStringTask;
            return contents.Length;
        }

        async Task<int> GetTaskOfResultAsync()
        {
            int hours = 0;
            await Task.Delay(0);
            return hours;
        }
        void DoIndependentWork()
        {
            Console.WriteLine("working......");
        }

        public static async Task DisplayCurrentInfoAsync()
        {
            await WaitAndApologizeAsync();
            Console.WriteLine($"Today is {DateTime.Now:D}");
            Console.WriteLine($"The current time is {DateTime.Now.TimeOfDay:t}");
            Console.WriteLine("The current temperature is 76 degress");
        }

        static async Task WaitAndApologizeAsync()
        {
            await Task.Delay(2000);
            Console.WriteLine("Sorry for the delay...\n");
        }
    }
}
